namespace DotNetNuke.HtmlEditor
{
    using DotNetNuke.Common;
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Framework;
    using DotNetNuke.Security;
    using FreeTextBoxControls;
    
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.IO;
    using System.Runtime.CompilerServices;

    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The FTBImageGallery Class provides the Image Gallery for the FTB Provider
    /// </summary>
    /// <remarks>
    /// </remarks>
    /// <history>
    /// [cnurse]	12/14/2004  documented and updated to work with FTB3.0
    /// </history>
    /// -----------------------------------------------------------------------------
    public class FTBImageGallery : PageBase
    {
        private static List<WeakReference> __ENCList = new List<WeakReference>();
        //("imgGallery")]
        private ImageGallery _imgGallery;

       
        public FTBImageGallery()
        {
            base.PreRender += new EventHandler(this.Page_PreRender);
            base.Load += new EventHandler(this.Page_Load);
            List<WeakReference> list = __ENCList;
            lock (list)
            {
                __ENCList.Add(new WeakReference(this));
            }
        }

        private void Page_Load(object sender, EventArgs e)
        {
            bool AllowAccess;
            string strTitle = this.PortalSettings.PortalName + " > Image Gallery";
            if (Globals.GetHashValue((Globals.HostSettings["Copyright"]), "Y") == "Y")
            {
                strTitle = strTitle + " ( DNN " + this.PortalSettings.Version + " )";
            }
            this.Title = strTitle;
            try
            {
                string strCurrentFolder = this.Server.MapPath(this.imgGallery.CurrentImagesFolder);
                if (!strCurrentFolder.EndsWith(@"\"))
                {
                    strCurrentFolder = strCurrentFolder + @"\";
                }
                if (this.PortalSettings.ActiveTab.ParentId == this.PortalSettings.SuperTabId)
                {
                    strCurrentFolder = strCurrentFolder.Substring(Globals.HostMapPath.Length);
                }
                else
                {
                    strCurrentFolder = strCurrentFolder.Substring(this.PortalSettings.HomeDirectoryMapPath.Length);
                }
                if (strCurrentFolder.EndsWith(@"\"))
                {
                    strCurrentFolder = strCurrentFolder.Substring(0, strCurrentFolder.Length - 1);
                }
                if (PortalSecurity.IsInRoles(FileSystemUtils.GetRoles(strCurrentFolder.Replace(@"\", "/"), this.PortalSettings.PortalId, "WRITE")))
                {
                    AllowAccess = true;
                }
                else
                {
                    AllowAccess = false;
                }
            }
            catch (Exception exception1)
            {

                Exception ex = exception1;
                AllowAccess = false;

            }
            this.imgGallery.AllowDirectoryCreate = AllowAccess;
            this.imgGallery.AllowDirectoryDelete = AllowAccess;
            this.imgGallery.AllowImageDelete = AllowAccess;
            this.imgGallery.AllowImageUpload = AllowAccess;
            this.imgGallery.JavaScriptLocation = ResourceLocation.ExternalFile;
            this.imgGallery.UtilityImagesLocation = ResourceLocation.ExternalFile;
            this.imgGallery.SupportFolder = Globals.ResolveUrl("~/Providers/HtmlEditorProviders/Ftb3HtmlEditorProvider/ftb3/");
        }

        private void Page_PreRender(object sender, EventArgs e)
        {
            string strDirectory;
            string[] strDirectories = Directory.GetDirectories(this.Server.MapPath(this.imgGallery.CurrentImagesFolder), "*");
            ArrayList alDirectories = new ArrayList();
            int refIntHelperL0 = strDirectories.Length - 1;
            for (int i = 0; i <= refIntHelperL0; i++)
            {
                if (this.PortalSettings.ActiveTab.ParentId == this.PortalSettings.SuperTabId)
                {
                    strDirectory = strDirectories[i].Substring(Globals.HostMapPath.Length);
                }
                else
                {
                    strDirectory = strDirectories[i].Substring(this.PortalSettings.HomeDirectoryMapPath.Length);
                }
                if (PortalSecurity.IsInRoles(FileSystemUtils.GetRoles(strDirectory.Replace(@"\", "/"), this.PortalSettings.PortalId, "READ")))
                {
                    alDirectories.Add(strDirectory);
                }
            }
            this.imgGallery.CurrentDirectories = (string[]) alDirectories.ToArray(typeof(string));
            string currentfolder = this.Server.MapPath(this.imgGallery.CurrentImagesFolder);
            if (!currentfolder.EndsWith(@"\"))
            {
                currentfolder = currentfolder + @"\";
            }
            if (this.PortalSettings.ActiveTab.ParentId == this.PortalSettings.SuperTabId)
            {
                strDirectory = currentfolder.Substring(Globals.HostMapPath.Length);
            }
            else
            {
                strDirectory = currentfolder.Substring(this.PortalSettings.HomeDirectoryMapPath.Length);
            }
            if (strDirectory.EndsWith(@"\"))
            {
                strDirectory = strDirectory.Substring(0, strDirectory.Length - 1);
            }
            if (!PortalSecurity.IsInRoles(FileSystemUtils.GetRoles(strDirectory.Replace(@"\", "/"), this.PortalSettings.PortalId, "READ")))
            {
                this.imgGallery.CurrentImages = new FileInfo[0];
            }
        }

        protected virtual ImageGallery imgGallery
        {
           
            get
            {
                return this._imgGallery;
            }
            
            set
            {
                this._imgGallery = value;
            }
        }
    }
}

